<?php
namespace app\common\model;
use think\Model;
use think\facade\Cache;
use think\facade\Db;


/**
 * TODO 用户消费新增金额明细 model
 * Class UserBill
 * @package app\models\user
 */
class UserBill extends Model
{

   	//账单 - 加
	public static function income( $title, $uid, $category, $type, $number, $link_id = 0, $balance = 0,$mark = '',$brokerage=0,$status=1 )
    {
    	if(!$number || $number <= 0) return true;
		$pm = 1;
		$shop_id = model('user')->where('id',$uid)->value('shop_id');
        return self::create(compact(
        	'title','uid','link_id','category','type','number','balance','mark','status','pm','brokerage',
        	'shop_id'
		));
    }
	
	//账单 - 减
    public static function expend( $title,$uid,$category,$type,$number,$link_id= 0,$balance = 0,$mark = '',$brokerage=0,$status=1 )
    {
    	if(!$number || $number <= 0) return true;
		$pm = 0;
		$shop_id = model('user')->where('id',$uid)->value('shop_id');	
        return self::create(compact(
        	'title','uid','link_id','category','type','number','balance','mark','status','pm','brokerage',
        	'shop_id'
		));
    }

    /**
     * 积分/佣金 使用记录
     * @param $uid
     */
    public static function userBillList($uid,$category='integral')
    {
    	$model = self::alias('a');
		$model = $model->leftJoin('user b','a.uid = b.id');
		$model = $model->field('a.id,a.link_id,a.title,a.pm,a.add_time,a.number,a.brokerage,a.mark');
		$model = $model->where('a.uid',$uid);
		$model = $model->where('a.status',1);
		$model = $model->where(function($query){
			$query = $query->where(function($query){
				$query->where('a.category','balance');
			});
			$query = $query->whereor(function($query){
				$query->where('a.category','<>','balance');
				//$query->where('a.shop_id = b.shop_id ');
			});
		});
		if($category!=='*'){
			$model = $model->where('a.category','in',$category);
		}
		$model = $model->order('a.add_time','desc');
		$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
		$list = $model->select()->toArray();
        foreach ($list as &$v){
            $v['add_time'] = date('Y.m.d H:i',strtotime($v['add_time']) );
            $v['number'] = floatval($v['number']);
            $v['brokerage'] = floatval($v['brokerage']);
        }
        return $list;
    }

    /**
     * 获取昨日佣金
     * @param $uid
     * @return float
     */
    public static function yesterdayCommissionSum($uid)
    {
        return self::where('uid',$uid)->where('category','balance')->where('type','brokerage')->where('pm',1)
            ->where('status',1)->whereTime('add_time', 'yesterday')->sum('number');
    }

    /**
     * 获取总佣金
     * @param $uid
     * @return float
     */
    public static function getBrokerage($uid)
    {
        return self::where('uid',$uid)->where('category','balance')->where('type','brokerage')->where('pm',1)
            ->where('status',1)->sum('number');
    }

    /**
     * 获取后台添加的余额
     * @param $uid
     * @return float
     */
    public static function getSystemAdd($uid)
    {
        return self::where('uid',$uid)->where('category','balance')->where('type','system_add')->where('pm',1)
            ->where('status',1)->sum('number');
    }


    /**
     * 累计充值
     * @param $uid
     * @return float
     */
    public static function getRecharge($uid)
    {
        return self::where('uid', $uid)
            ->where('category', 'balance')
            ->where('type', 'recharge')
            ->where('pm', 1)
            ->where('status', 1)
            ->sum('number');
    }


    /**
     * TODO 获取用户记录总和
     * @param $uid
     * @param string $category
     * @param string $type
     * @return mixed
     */
    public static function getRecordCount($uid, $category = 'balance', $type = '',$time=''){
        $model = new self;
        $model = $model->where('uid', $uid);
        $model = $model->where('category',$category);
        $model = $model->where('status',1);
        if(strlen(trim($type))) $model = $model->where('type','in',$type);
        if($time) $model=$model->whereTime('add_time',$time);
        return $model->sum('number');
    }

    /**
     * TODO 获取订单返佣记录总数
     * @param $uid
     * @param string $category
     * @param string $type
     * @return mixed
     */
    public static function getRecordOrderCount($uid, $category = 'balance', $type = 'brokerage'){
        $model = new self;
        $model = $model->where('uid', $uid);
        $model = $model->where('category',$category);
        if(strlen(trim($type))) $model = $model->whereIn('type', $type);
        return $model->count();
    }

    /*
     * 记录分享次数
     * @param int $uid 用户uid
     * @param int $cd 冷却时间
     * @return Boolean
     * */
    public static function setUserShare($uid,$cd=300){
        $user=User::where('uid',$uid)->find();
        if(!$user) return self::setErrorInfo('用户不存在！');
        $cachename='Share_'.$uid;
        if(Cache::has($cachename)) return false;
        self::income('用户分享记录',$uid,'share','share',1,0,0,date('Y-m-d H:i:s',time()).':用户分享');
        Cache::set($cachename,1,$cd);
        event('UserLevelAfter',[$user]);
        return true;
    }

}
